package kit.views;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import kit.persistency.KitDB;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class QuestionPanel extends KitPanel {
	private JTextArea questionText;

	/**
	 * Create the panel.
	 */
	public QuestionPanel() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		add(panel, BorderLayout.SOUTH);
		
		JButton btnNewButton = new JButton("Salva");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				KitDB db = new KitDB();
				String question = QuestionPanel.this.getQuestionText().getText().trim();
				if(question.trim() == "")
					return;
				KitFrame frame = (KitFrame) SwingUtilities.getRoot(QuestionPanel.this);
				if(!db.getSecrets().contains(question))
					db.insertSecret(question);
				else
					QuestionPanel.this.showErrorDialog("La domanda è già presente", frame);

				frame.populateQuestionsList();
				frame.repaint();
				frame.getQuestionList().setSelectedIndex(0);
			}
		});
		panel.add(btnNewButton);
		
		JPanel panel_1 = new JPanel();
		add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(141dlu;default):grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(38dlu;default):grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel label = new JLabel("Testo domanda:");
		panel_1.add(label, "2, 2, default, top");
		
		questionText = new JTextArea();
		questionText.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_1.add(questionText, "4, 2, fill, fill");

	}

	protected JTextArea getQuestionText() {
		return questionText;
	}
}
